/**
 * 
 */
package org.swing.utility.system.flatform;
import java.lang.reflect.Method;
import java.net.*;

/**
 * @author Le Quynh Nhu
 *
 */
public class SystemUtilities {

    public static URL getURL(String path) {

        class ClassLoaderReference {

            public ClassLoader getClassLoader() {
                ClassLoader cl = getClass().getClassLoader();
                return cl;
            }
        }

        ClassLoaderReference clr = new ClassLoaderReference();
        ClassLoader cl = clr.getClassLoader();
        return cl.getResource(path);

    }

    public static String getFile(String path) {
        URL t = SystemUtilities.getURL(path);
        return SystemUtilities.getURL(path).getFile();
    }

    public static boolean isMacOS() {
        String osName = System.getProperty("os.name");
        return osName.startsWith("Mac OS");
    }

    public static boolean isWindowsOS() {
        String osName = System.getProperty("os.name");
        return osName.startsWith("Windows");
    }

    /**
     * Set a few common properties for the given application if we are running
     * under MacOS. Usage Example:
     *
     * <pre>
     * if (MacOSUtil.isMacOS()) {
     *   MacOSUtil.configureMacOSApplication(&quot;JabaDex&quot;);
     * }
     * </pre>
     *
     * @param appName -
     *            the name of the Application.
     */
    public static void configureMacOSApplication(String appName) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("apple.awt.showGrowBox", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", appName);
    }

    /**
     * Open the systems web browser.
     * 
     * @param url
     */
    public static void openBrowserWithURL(URL url) {
        String osName = System.getProperty("os.name");
        try {
            if (isMacOS()) {
                Class fileMgr = Class.forName("com.apple.eio.FileManager");
                Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[]{String.class});
                openURL.invoke(null, new Object[]{url.toExternalForm()});
            }
            else if (osName.startsWith("Windows")) {
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            }
            else { //assume Unix or Linux

                String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"};
                String browser = null;
                for (int count = 0; count < browsers.length && browser == null; count++) {
                    if (Runtime.getRuntime().exec(
                            new String[]{"which", browsers[count]}).waitFor() == 0) {
                        browser = browsers[count];
                    }
                }

                if (browser == null) {
                    throw new RuntimeException("Could not find web browser");
                }
                else {
                    Runtime.getRuntime().exec(new String[]{browser, url.toExternalForm()});
                }
            }
        }
        catch (Exception e) {
            throw new RuntimeException("Failed to open web browser", e);
        }
    }

    /**
     * Handle method for locating the jar that contains a particular class
     *
     * @param The class of interest
     * @return the name of the jar file containing the class
     */
    public static String findJarForClass(Class clazz) {
        String name = clazz.getName();
        name = name.substring(name.lastIndexOf('.') + 1);
        String jar = clazz.getResource(name + ".class").toString(); //NOI18N
        return jar.substring(0, jar.indexOf('!'));
    }
}